/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package module;

import java.util.ArrayList;
import wargame.dao.AnalyticEmployeeUtilisationDAO;
import wargame.dao.AnalyticProjectDAO;
import wargame.dao.EmployeeDAO;
import wargame.dao.PlayerDAO;
import wargame.dao.TurnInformationDAO;
import wargame.entity.AnalyticEmployeeUtilisation;
import wargame.entity.AnalyticProject;
import wargame.entity.Employee;
import wargame.entity.Player;

/**
 *
 * @author Clarence
 */
public class Analytic {

    public static void createEmployeeUtilisationAnalytic() {
        ArrayList<Player> players = PlayerDAO.retrieveAllPlayers();

        for (int i = 0; i < players.size(); i++) {
            Player p = players.get(i);
            ArrayList<Employee> employees = EmployeeDAO.retrievePlayerEmployees(p);

            int totalHoursAvailable = 0;
            int totalHoursUsed = 0;

            for (int j = 0; j < employees.size(); j++) {
                Employee e = employees.get(j);


                if (e.getTurn_hired() < GameTurn.getTurnInfo().getCurrent_turn()) { // I don't want to include the 50 hours at the turn of hiring the employee
                    totalHoursAvailable += GlobalConstant.HOURS_PER_TURN;
                    totalHoursUsed += GlobalConstant.HOURS_PER_TURN - e.getHours_remaining();
                }
            }
            AnalyticEmployeeUtilisation aeu = new AnalyticEmployeeUtilisation();
            aeu.setPlayer_id(p.getPlayer_id());
            aeu.setEmployee_available_hours(totalHoursAvailable);
            aeu.setEmployee_hours_used(totalHoursUsed);
            aeu.setTurn(GameTurn.getTurnInfo().getCurrent_turn());

            AnalyticEmployeeUtilisationDAO.insertAnalyticEmployeeUtilisation(aeu);
        }
    }

    public static void createBidAttemptAnalytic(int player_id, int bid_amount, int project_max_bid) {
        AnalyticProject ap = new AnalyticProject();
        ap.setPlayer_id(player_id);
        ap.setAttempt_bid(1);
        ap.setProject_bid_amount(bid_amount);
        ap.setProject_max_bid(project_max_bid);
        ap.setTurn(GameTurn.getTurnInfo().getCurrent_turn());
        AnalyticProjectDAO.insertAnalyticProject(ap);
    }

    public static void createSuccessBidAnalytic(int player_id, int bid_amount, int project_max_bid) {
        AnalyticProject ap = new AnalyticProject();
        ap.setPlayer_id(player_id);
        ap.setSuccessful_bid(1);
        ap.setProject_bid_amount(bid_amount);
        ap.setProject_max_bid(project_max_bid);
        ap.setTurn(GameTurn.getTurnInfo().getCurrent_turn());
        AnalyticProjectDAO.insertAnalyticProject(ap);
    }

    public static void createSubmitProjectAnalytic(int player_id, boolean isOnTime) {
        AnalyticProject ap = new AnalyticProject();
        ap.setPlayer_id(player_id);
        ap.setProject_submitted(1);
        if (isOnTime == true) {
            ap.setProject_submitted_late(0);
            ap.setProject_submitted_on_time(1);
        } else {
            ap.setProject_submitted_late(1);
            ap.setProject_submitted_on_time(0);
        }
        ap.setTurn(GameTurn.current_turn);
        AnalyticProjectDAO.insertAnalyticProject(ap);
    }

    public static void createTakeOutsourceProjectAnalytic(int player_id) {
        AnalyticProject ap = new AnalyticProject();
        ap.setPlayer_id(player_id);
        ap.setOutsourced_project_purchased(1);
        ap.setTurn(GameTurn.getTurnInfo().getCurrent_turn());
        AnalyticProjectDAO.insertAnalyticProject(ap);
    }

    public static void createSubmitOutsourceProjectAnalytic(int player_id, boolean isOnTime) {
        AnalyticProject ap = new AnalyticProject();
        ap.setPlayer_id(player_id);
        ap.setOutsourced_project_submitted(1);
        if (isOnTime == true) {
            ap.setOutsourced_project_submitted_late(0);
            ap.setProject_submitted_on_time(1);
        } else {
            ap.setOutsourced_project_submitted_late(1);
            ap.setProject_submitted_on_time(0);
        }
        ap.setTurn(GameTurn.getTurnInfo().getCurrent_turn());
        AnalyticProjectDAO.insertAnalyticProject(ap);
    }

    public static double getProjectBidMargin(int player_id, int turn) {
        //return AnalyticProjectDAO.getProjectBidTotal(player_id, turn);
        
        double bid_total =  AnalyticProjectDAO.getProjectBidTotal(player_id, turn);
        double bidmax_total = AnalyticProjectDAO.getProjectMaxTotal(player_id, turn);


        if (bid_total == 0 ||bidmax_total == 0) {

            return 0.0;
        }
        else {
            double project_margin = bid_total / bidmax_total;
            return project_margin;
        }
    }
    

}
